from tabnanny import verbose
from django.db import models
from django.utils import timezone
#导入默认的用户模型
from django.contrib.auth.models import User
# Create your models here.
# 产品模型
class Goods(models.Model):
    # 新鲜好物模型
    title = models.CharField(max_length=100,verbose_name="商品标题")
    subtitle = models.CharField(max_length=100,verbose_name="商品副标题")
    price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="商品价格")
    original_price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name="商品原价",blank=True,null=True)
    image = models.ImageField(upload_to='goods/',verbose_name='商品图片')
    description = models.TextField(verbose_name="商品描述")
    detail_url = models.URLField(verbose_name="商品详情页地址")
    is_active = models.BooleanField(default=True,verbose_name="是否激活")
    sort_order = models.IntegerField(default=0,verbose_name="排序")
    create_at = models.DateTimeField(default=timezone.now,verbose_name="创建时间")
    update_at = models.DateTimeField(default=timezone.now,verbose_name="更新时间")
    class Meta:
        verbose_name = '新鲜好物'
        verbose_name_plural = verbose_name
        ordering=['-create_at','sort_order']
    def __str__(self):
        return self.title

#用户模型
class AdminProfile(models.Model):
    """管理员模型"""
    # 关联用户，参数的意思分别是 关联的模型，删除关联的模型时，本模型也删除，related_name是反向引用
    user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='admin_profile',verbose_name='用户')
    avatar = models.ImageField(upload_to='avatar/',blank=True,null=True,verbose_name='头像')
    email = models.EmailField(verbose_name='邮箱')
    phone = models.CharField(max_length=11,blank=True,verbose_name='手机号')
    department = models.CharField(max_length=50,blank=True,verbose_name='部门')
    create_at = models.DateTimeField(default=timezone.now,verbose_name='创建时间')
    class Meta:
        verbose_name = '管理员资料'
        verbose_name_plural = verbose_name
# 管理员日志模型
class AdminLog(models.Model):
    admin = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='管理员')
    action = models.CharField(max_length=100,verbose_name='操作')
    description = models.TextField(verbose_name='描述')
    # IP地址,其中GenericIPAddressField是Django提供的一个字段，用于保存IP地址
    ip_address = models.GenericIPAddressField(verbose_name='IP地址')
    create_at = models.DateTimeField(default=timezone.now,verbose_name='创建时间')
    class Meta:
        ordering = ['-create_at']
        verbose_name = '管理员日志'
        verbose_name_plural = verbose_name  


